/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.zprofilefrontend.common;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author namhh
 */
public class Validation {

    public static final Pattern ZPROFILE_USERNAME_PATTERN = Pattern.compile("^[a-zA-Z_-_.]{3,20}$");
    public static final Pattern ZPROFILE_PASSWORD_PATTERN = Pattern.compile("^.{6,30}$");
    public static final Pattern ZPROFILE_DISPLAY_NAME_PATTERN = Pattern.compile("^.{0,50}$");
    public static final Pattern ZPROFILE_EMAIL_PATTERN = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
    public static final Pattern ZPROFILE_MOBILE_PATTERN = Pattern.compile("^(?:0084|\\+84|0)[0-9]{9,10}$");

//    public static String INVALID_USERNAME = "Your username is not valid. Only characters A-Z, a-z , ' - ' and '.' and between 3 and 20 characters.<br/>";
//    public static String INVALID_PASSWORD = "Your password is not valid. Its length is  between 6-30 characters.<br/>";
//    public static String INVALID_DISPLAYNAME = "Your display name is not valid.<br/>";
//    public static String INVALID_DATEOFBIRTH = "Your date of birth is not valid.<br/>";
//    public static String INVALID_GENDER = "Please select one gender.<br/>";
//    public static String INVALID_EMAIL = "Your email address is not valid.<br/>";
//    public static String INVALID_MOBILE = "Your mobile is not valid.<br/>";
//    public static String USERNAME_EXIST = "Username exists.<br/>";

    public static boolean validateUsername(String userame) {
        Matcher matcher = ZPROFILE_USERNAME_PATTERN.matcher(userame);
        return matcher.find();
    }

    public static boolean validatePassword(String password) {
        Matcher matcher = ZPROFILE_PASSWORD_PATTERN.matcher(password);
        return matcher.find();
    }

    public static boolean validateDisplayName(String displayName) {
        if ("".equals(displayName)) {
            return true;
        }
        Matcher matcher = ZPROFILE_DISPLAY_NAME_PATTERN.matcher(displayName);
        return matcher.find();
    }

    public static boolean validateEmail(String email) {
        if ("".equals(email)) {
            return true;
        }
        Matcher matcher = ZPROFILE_EMAIL_PATTERN.matcher(email);
        return matcher.matches();
    }

    public static boolean validateMobile(String mobile) {
        if ("".equals(mobile)) {
            return true;
        }
        Matcher matcher = ZPROFILE_MOBILE_PATTERN.matcher(mobile);
        return matcher.find();
    }

    public static boolean validateDateOfBirth(Integer dateOfBirth) {
        if (null == dateOfBirth) {
            return false;
        }
        Long now = new Date().getTime() / 1000;
        return dateOfBirth < now.intValue();
    }

}
